﻿using System;
using System.ServiceProcess;
using System.Threading;
using System.Threading.Tasks;

namespace PanoramicData.TickTockBlink.Service
{
	public partial class Service : ServiceBase
	{
		private Task _task;
		private CancellationTokenSource _tokenSource;

		public Service()
		{
			InitializeComponent();
		}

		protected override void OnStart(string[] args)
		{
			_tokenSource = new CancellationTokenSource();

			Console.CancelKeyPress += delegate
			{
				Console.WriteLine("Ctrl-C pressed");
				_tokenSource.Cancel();
			};

			_task = new Task(()=>TickTockBlinkProgram.MainLoop(_tokenSource.Token), _tokenSource.Token);
			_task.Start();
		}

		protected override void OnStop()
		{
			_tokenSource.Cancel();
			try
			{
				_task.Wait();
			}
			catch (Exception e)
			{
				// Do nothing
			}
		}
	}
}
